﻿using IdentityServer4.EntityFramework.DbContexts;
using IdentityServer4.EntityFramework.Options;
using K9Nano.Share.Extensions;
using Microsoft.EntityFrameworkCore;

namespace K9Nano.IdentityServerCore.Data
{
    public class K9PersistedGrantDbContext : PersistedGrantDbContext<K9PersistedGrantDbContext>
    {
        public K9PersistedGrantDbContext(DbContextOptions<K9PersistedGrantDbContext> options, OperationalStoreOptions storeOptions) : base(options, storeOptions)
        {
        }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            foreach (var entityType in modelBuilder.Model.GetEntityTypes())
            {
                // Naming convention
                var entityBuilder = modelBuilder.Entity(entityType.Name);
                var currentTableName = entityBuilder.Metadata.GetTableName();
                entityBuilder.ToTable("idsvr_" + currentTableName.ToSingular().ToSnakeCase());
            }
        }
    }
}